package com.http;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

public class HttpRemoteInvoke extends AbstractHttpClientPool{

	private static final AbstractHttpClientPool noticefyHttp = new HttpRemoteInvoke("utf-8",120000);
	
	public static final ThreadLocal<DateFormat>  df = new ThreadLocal<DateFormat>(){
		@Override
		protected DateFormat initialValue(){
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
	};

	
	private static final Logger LOGGER = Logger.getLogger(HttpRemoteInvoke.class);
	
	private HttpRemoteInvoke(String enCode,int timeOut){
		super(enCode,timeOut,50);
	}
	
	@Override
	protected  void config(HttpRequestBase httpRequestBase) {
        // 设置Header等
        // httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");application/x-www-form-urlencoded,
         httpRequestBase
        .setHeader("Accept",
         "text/html,application/xhtml+xml," +
         "application/xml;q=0.9,*/*;q=0.8");
         httpRequestBase.setHeader("Accept-Language",
         "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");// "en-US,en;q=0.5");
         httpRequestBase.setHeader("Accept-Charset",
         "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
        // 配置请求的超时设置
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(timeOut)
                .setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
        httpRequestBase.setConfig(requestConfig);
    }

	@Override
	 protected  void setPostParams(HttpPost httpost,
	            Map<String, Object> params) {
	    	if(params !=null){
	    		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	            for (Map.Entry<String, Object> temp: params.entrySet()) {
	                nvps.add(new BasicNameValuePair(temp.getKey(), (String)temp.getValue()));
	            }
	            try {
	            
	                httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
	            } catch (UnsupportedEncodingException e) {
	                e.printStackTrace();
	            }
	    	}
	    }
	
	public static void noticeJson(final String url,final JSONObject json){
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					String returnStr = "";
					if(url!=null){
					    returnStr = noticefyHttp.doPost(url, json);	
					}
					LOGGER.info("----------返回的通知---"+returnStr);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					LOGGER.error("调用接口通知失败",e);
				}
			}
		}).start();
	}
	public static void notice(final String url,final Map<String, Object> params){
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					String returnStr = "";
					if(url!=null){
					    returnStr = noticefyHttp.post(url, params);	
					}
					LOGGER.info("----------返回的通知---"+returnStr);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					LOGGER.error("调用接口通知失败",e);
				}
			}
		}).start();
	}
	
	public static String invokeApiGet(final String url) {
		String resultStr = noticefyHttp.get(url);
		return resultStr;
	}
}
